package com.example.studentmanagersystem;

import com.example.studentmanagersystem.R;
import com.example.studentmanagersystem.R.layout;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * @version 1.0
 * @author cwf
 * data 2015-11-12
 *
 */
public class TeacherMainActivity extends Activity implements OnClickListener {

	private Button personalBtn,stuGradeBtn,actionBarBtn,actionMenu;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.teacher_main_activity);
		this.init();
	}
	
	/**
	 * initialize widget
	 */
	 private void init() {
		this.actionBarBtn=(Button) this.findViewById(R.id.tecMain_action_backButton);
		this.actionMenu=(Button) this.findViewById(R.id.tecMain_action_menu);
		
		this.personalBtn=(Button) this.findViewById(R.id.teacher_main_activity_tecInfoChange);
		this.stuGradeBtn=(Button) this.findViewById(R.id.teacher_main_activity_stuGradeChange);
		
		this.actionBarBtn.setOnClickListener(this);
		this.actionMenu.setOnClickListener(this);
		
		this.personalBtn.setOnClickListener(this);
		this.stuGradeBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		int id=v.getId();
		switch (id) {
		//when you click the update info button which is teacher,the method will be called当点击老师界面个人信息修改按钮时，调用此方法
		case R.id.teacher_main_activity_tecInfoChange:
			intent=new Intent(this,TeacherInfoChangeActivity.class);
			this.startActivity(intent);
			break;
		//when you click the update info button which is student,the method will be called当点击学生成绩修改界面时，调用此方法
		case R.id.teacher_main_activity_stuGradeChange:
			intent=new Intent(this,TeacherSetGradeActivity.class);
			this.startActivity(intent);
			break;
			//when you click the back button, the method will be called当点击导航栏返回按钮时，调用此方法
		case R.id.tecMain_action_backButton:
				
			exit();
				
				break;
		case R.id.tecMain_action_menu:
				
			PopupMenuUtil popupMenuUtil=new PopupMenuUtil(v,this);
			popupMenuUtil.actionBackListener();
			
				break;
		default:
			break;
		}
		
	}
	
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		
		 if (keyCode == KeyEvent.KEYCODE_BACK) {  
	            exit();  
	            return false;  
	        } else {  
	            return super.onKeyDown(keyCode, event);  
	        }  
	}

	boolean isExit;
	private void exit() {
		 
		if (!isExit) {  
            isExit = true;  
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();  
            mHandler.sendEmptyMessageDelayed(0, 2000);  
        } else {  
            Intent intent = new Intent(Intent.ACTION_MAIN);  
            intent.addCategory(Intent.CATEGORY_HOME);  
            startActivity(intent);  
            this.finish();  
        }  
		
	}
	
	Handler mHandler = new Handler() {  
		  
        @Override  
        public void handleMessage(Message msg) {  
            // TODO Auto-generated method stub  
            super.handleMessage(msg);  
            isExit = false;  
        }  
  
    }; 
	
	 
}
